package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.Car;
import com.gljx.web.Entity.Carmaintainentry;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.User;
import com.gljx.web.controller.PersonController;
import com.gljx.web.service.CarService;
import com.gljx.web.service.CarmainTainService;
import com.gljx.web.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class CarMaintainJournalController {

	@Autowired
	private CarmainTainService carMainTainService;
	
	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private CarService carService;
	
	@Autowired
	private PersonController PersonController;
	
	
	/**
	 * 车辆维修统计表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selCarMaintainJournal")
	public String selCarMaintainJournal(HttpServletRequest request) throws Exception{
		HttpSession session=request.getSession();
		Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
		 		 cale.add(Calendar.MONTH, 0);
		 		 cale.set(Calendar.DAY_OF_MONTH, 1);
		Date resultDate = cale.getTime(); // 结果
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String beginTime = sdf.format(resultDate);
		String endTime = sdf.format(new Date());
        User user = (User) session.getAttribute("user");
        Map map = PersonController.getPageAsJl(request, "", "", "0");
 		//查询教练组信息
        List<Car> cars = carService.queryCar(user.getFcompanyid());
        List<Department> departments = departmentService.selectAdminListAsfcontid("",user.getFcompanyid());
        Map<String , String> map3 = new HashMap<>();
        					 map3.put("beginTime", beginTime);
        					 map3.put("endTime", endTime);
        					 map3.put("companyId", user.getFcompanyid());
        List<Carmaintainentry> carmaintainentries = carMainTainService.selectCarMainTain(map3);
        Carmaintainentry carmaintainentry = carMainTainService.selectCarMainTainCount(map3);
        request.setAttribute("carmaintainentrys", carmaintainentry);
		request.setAttribute("persons", map.get("sList"));	
		request.setAttribute("pb", map.get("pb"));
		request.setAttribute("cars", cars);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("carmaintainentries", carmaintainentries);
		request.setAttribute("departmentList",departments);
		return "backHome/journaling/carMaintainJournaling/carMaintainJournal";		
	}
	
	
	/**
	 * 车辆维修统计表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selCarMaintainJournalByCon")
	public String selCarMaintainJournalByCon(HttpServletRequest request,String beginTime,String endTime,Carmaintainentry carmaintainentry,String deparmentId) throws Exception{
		HttpSession session=request.getSession();
        User user = (User) session.getAttribute("user");
        Map map = PersonController.getPageAsJl(request, "", "", "0");
 		//查询教练组信息
        List<Car> cars = carService.queryCar(user.getFcompanyid());
        List<Department> departments = departmentService.selectAdminList("");
        Map<String , String> map3 = new HashMap<>();
        					 map3.put("beginTime", beginTime);
        					 map3.put("endTime", endTime);
        					 map3.put("companyId", user.getFcompanyid());
        					 map3.put("type", carmaintainentry.getCfmaintetype());
        					 map3.put("car", carmaintainentry.getCfcarnoid());
        					 map3.put("deparment", deparmentId);
        List<Carmaintainentry> carmaintainentries = carMainTainService.selectCarMainTain(map3);
        Carmaintainentry carmaintainentry2 = carMainTainService.selectCarMainTainCount(map3);
        request.setAttribute("carmaintainentrys", carmaintainentry2);
		request.setAttribute("persons", map.get("sList"));	
		request.setAttribute("pb", map.get("pb"));
		request.setAttribute("cars", cars);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("deparmentId", deparmentId);
		request.setAttribute("carmaintainentry", carmaintainentry);
		request.setAttribute("carmaintainentries", carmaintainentries);
		request.setAttribute("departmentList",departments);
		return "backHome/journaling/carMaintainJournaling/carMaintainJournal";		
	}
	
}
